﻿using System.Collections;
using Selenium;

namespace QAHelper.Common
{
    public static class SeleniumInputHelper
    {
        public static void SetPageInput(ISelenium selenium, DictionaryEntry[] input_data)
        {
            foreach (DictionaryEntry dataRow in input_data)
            {
                if (dataRow.Key is System.DBNull && dataRow.Value is System.DBNull)
                    continue;
                string control_name = dataRow.Key.ToString();
                string control_value = dataRow.Value.ToString();

                if (selenium.IsElementPresent(control_name))
                {
                    try
                    {
                        selenium.Type(control_name, control_value);
                    }
                    catch (SeleniumException se)
                    {
                        selenium.Select(control_name, "label="+control_value);
                    }
                }
            }
        }

        #region Method with 3rd parameter which is a out parameter.

        public static void SetPageInput(ISelenium selenium, DictionaryEntry[] input_data, out int no_of_input_fields)
        {
            no_of_input_fields = 0;

            foreach (DictionaryEntry dataRow in input_data)
            {
                if (dataRow.Key is System.DBNull && dataRow.Value is System.DBNull)
                    continue;
                string control_name = dataRow.Key.ToString();
                string control_value = dataRow.Value.ToString();

                if (selenium.IsElementPresent(control_name))
                {
                    try
                    {
                        selenium.Type(control_name, control_value);
                        no_of_input_fields++;
                    }
                    catch (SeleniumException se)
                    {
                        selenium.Select(control_name, "label=" + control_value);
                        no_of_input_fields++;
                    }
                }
            }
        }

        #endregion
    }
}
